package com.diankangwo.controller;

import com.diankangwo.dao.QueryPaymentDAO;
import com.diankangwo.domain.PayManagement;
import com.diankangwo.domain.PayResult;
import com.diankangwo.domain.QueryPayment;
import com.diankangwo.dto.PayResultDTO;
import com.diankangwo.dto.QueryPaymentDTO;
import com.diankangwo.service.PayManagementService;
import com.diankangwo.service.PayResultService;
import com.diankangwo.util.AmountUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 缴费查询的Controller
 * 
 * @Songrongye
 */
@Controller
@RequestMapping(value = "/b-manage")
public class QueryPaymentController {

	@Autowired
	private PayResultService payResultService;
	@Autowired
	private PayManagementService payManagementService;
	@Autowired
	private QueryPaymentDAO queryPaymentDAO;

	/**
	 * 索引所有缴费结果里信息并且分页
	 * 
	 * @throws Exception
	 * @throws NumberFormatException
	 */
	@RequestMapping(value = "/findAllResult")
	public ModelAndView findAllResuts(Integer pageNo, String billNo2, String billNo)
			throws NumberFormatException, Exception {

		ModelAndView view;
		view = new ModelAndView("/b-manage/financial2");
		PayResult payResultk = new PayResult();
		PayManagement payManagement = new PayManagement();
		List<PayResult> payResults = payResultService.queryAll();
		if (payResults.size() != 0) {
			for (PayResult payResult : payResults) {
				QueryPaymentDTO queryPaymentDTO = new QueryPaymentDTO();
				queryPaymentDTO.setBillNo(payResult.getBillNo());// 订单号
				queryPaymentDTO.setIsTest("YES");

				queryPaymentDTO.setPayBill(Float.parseFloat((AmountUtils.changeF2Y(payResult.getTotalFee() + ""))));// 金额
				queryPaymentDTO.setPayTime(payResult.getPayTime());// 缴费时间
				queryPaymentDTO.setPaytitle(payResult.getTitle());// 订单标题

				if (queryPaymentDAO.findbyBillNo(payResult.getBillNo()) == null) {
					QueryPayment queryPayment = new QueryPayment();
					queryPayment.setBillNo(payResult.getBillNo());// 订单号
					queryPayment.setTitle(payResult.getTitle());// 订单标题
					queryPayment.setPayTime(payResult.getPayTime());// 缴费时间
					queryPayment.setTotalFee(Float.parseFloat((AmountUtils.changeF2Y(payResult.getTotalFee() + ""))));
					queryPaymentDAO.saveOrUpdate(queryPayment);// 金额
				}

			}

			if (pageNo != null && billNo2 != null && !billNo2.equals("")) {
				view.addObject("settings", queryPaymentDAO.searchQuertPayment(pageNo, billNo2));
				view.addObject("pageName", billNo2);
				view.addObject("queryPaymentDTOs", queryPaymentDAO.searchQuertPayment(pageNo, billNo2).getResultData());
			} else {

				if (billNo2 != null && !billNo2.equals("")) {
					if (pageNo == null) {
						pageNo = 1;
					}
					view.addObject("settings", queryPaymentDAO.searchQuertPayment(pageNo, billNo2));
					view.addObject("pageName", billNo2);
					view.addObject("queryPaymentDTOs",
							queryPaymentDAO.searchQuertPayment(pageNo, billNo2).getResultData());
				} else {
					if (pageNo == null) {
						pageNo = 1;
					}
					view.addObject("settings1", queryPaymentDAO.searchQuertPaymentN(pageNo));
					view.addObject("queryPaymentDTOs", queryPaymentDAO.searchQuertPaymentN(pageNo).getResultData());
				}
			}

			/* view.addObject("showPageNumber",showPageNumber); */

			if (billNo != null) {

				payResultk = payResultService.queryByBillNo(billNo);

				PayResultDTO prDTO = new PayResultDTO();
				BeanUtils.copyProperties(prDTO, payResultk);
				String totalFee = ""+payResultk.getTotalFee();
				
				prDTO.setTotalFee(AmountUtils.changeF2Y((totalFee)));
				view.addObject("payResultk", prDTO);
				return view;
			} else {
				view.addObject("payResultk", payResultk);
				return view;
			}
		}

		else {
			view = new ModelAndView("/b-manage/financial2blank");
			return view;

		}
	}

}
